package com.wmx.mp.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.wmx.mp.config.MybatisPlusConfig;
import com.wmx.mp.entity.BasAgency;
import com.wmx.mp.service.IBasAgencyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单位信息前端控制器
 * 1、分页查询必须配置分页拦截器{@link PaginationInnerInterceptor}才能生效 {@link MybatisPlusConfig}
 *
 * @author 汪茂雄
 * @since 2023-03-05
 */
@RestController
public class BasAgencyController {

    @Resource
    private IBasAgencyService basAgencyService;

    /**
     * 查询全部信息
     * http://localhost:8080/basAgency/findAllList?keyword=教育局
     *
     * @return
     */
    @GetMapping("/basAgency/findAllList")
    public List<BasAgency> findAllList(String keyword) {
        List<BasAgency> list = basAgencyService.findAllList(keyword);
        return list;
    }

    /**
     * 分页查询
     * http://localhost:8080/basAgency/pageList?page=1&count=2
     *
     * @param page
     * @param count
     * @return
     */
    @GetMapping("/basAgency/pageList")
    public IPage<BasAgency> pageList(Integer page, Integer count) {
        IPage<BasAgency> pageList = basAgencyService.pageList(page, count);
        return pageList;
    }

    /**
     * 批量新增保存。
     * 插入操作时：如果属性没有指定，或者指定值为null，则属性名称不会出现在 insert into sql中。
     * http://localhost:8080/basAgency/saveList
     *
     * <pre>
     *     [
     *     {
     *         "agencyCode": "001007",
     *         "agencyName": "江东省邮政局",
     *         "createTime": "2023-08-05 10:03:00",
     *         "isDelete": 2,
     *         "lastYearExpenditure": 23678887.19,
     *         "loc": "江东市007号",
     *         "updateTime": "2023-02-06 11:53:00"
     *     },
     *     {
     *         "agencyCode": "001008",
     *         "agencyName": "江东省工商局",
     *         "createTime": "2023-09-05 10:03:00",
     *         "isDelete": 2,
     *         "lastYearExpenditure": 43677887.39,
     *         "loc": "江东市008号",
     *         "updateTime": "2023-12-06 11:53:00"
     *     }
     * ]
     * </pre>
     *
     * @param BasAgencyList ：待新增保存的数据
     * @return
     */
    @PostMapping("/basAgency/saveList")
    public List<BasAgency> saveList(@RequestBody List<BasAgency> BasAgencyList) {
        if (BasAgencyList == null || BasAgencyList.isEmpty()) {
            return basAgencyService.findAllList(null);
        }
        BasAgencyList.stream().forEach(item -> {
            item.setIsDelete(2);
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
        });
        basAgencyService.saveList(BasAgencyList);
        return basAgencyService.findAllList(null);
    }

    /**
     * 批量更新保存
     * http://localhost:8080/basAgency/updateList
     *
     * <pre>
     *     [
     *     {
     *         "agencyCode": "001001",
     *         "agencyId": 1,
     *         "agencyName": "江东省财政局",
     *         "createTime": "2023-08-04 10:00:00",
     *         "isDelete": 2,
     *         "lastYearExpenditure": 23678887.19,
     *         "loc": "江东市001号",
     *         "updateTime": "2023-02-06 11:50:00"
     *     },
     *     {
     *         "agencyCode": "001002",
     *         "agencyId": 2,
     *         "agencyName": "江东省教育局",
     *         "createTime": "2023-09-04 10:00:00",
     *         "isDelete": 2,
     *         "lastYearExpenditure": 43677887.39,
     *         "loc": "江东市002号",
     *         "updateTime": "2023-12-06 11:50:00"
     *     }
     * ]
     * </pre>
     *
     * @param BasAgencyList ：待更新保存的数据
     * @return
     */
    @PostMapping("/basAgency/updateList")
    public List<BasAgency> updateList(@RequestBody List<BasAgency> BasAgencyList) {
        if (BasAgencyList == null || BasAgencyList.isEmpty()) {
            return basAgencyService.findAllList(null);
        }
        BasAgencyList.stream().forEach(item -> item.setUpdateTime(new Date()));
        basAgencyService.updateList(BasAgencyList);
        return basAgencyService.findAllList(null);
    }

    /**
     * 批量删除保存
     * http://localhost:8080/basAgency/deleteList
     *
     * <pre>
     *     [
     *     {
     *         "agencyCode": "001001",
     *         "agencyId": 1,
     *         "agencyName": "江东省财政局",
     *         "createTime": "2023-08-04 10:00:00",
     *         "isDelete": 2,
     *         "lastYearExpenditure": 23678887.19,
     *         "loc": "江东市001号",
     *         "updateTime": "2023-02-06 11:50:00"
     *     },
     *     {
     *         "agencyCode": "001002",
     *         "agencyId": 2,
     *         "agencyName": "江东省教育局",
     *         "createTime": "2023-09-04 10:00:00",
     *         "isDelete": 2,
     *         "lastYearExpenditure": 43677887.39,
     *         "loc": "江东市002号",
     *         "updateTime": "2023-12-06 11:50:00"
     *     }
     * ]
     * </pre>
     *
     * @param BasAgencyList ：待删除的数据
     * @return
     */
    @PostMapping("/basAgency/deleteList")
    public List<BasAgency> deleteList(@RequestBody List<BasAgency> BasAgencyList) {
        if (BasAgencyList == null || BasAgencyList.isEmpty()) {
            return basAgencyService.findAllList(null);
        }
        BasAgencyList.stream().forEach(item -> item.setUpdateTime(new Date()));
        basAgencyService.deleteList(BasAgencyList);
        return basAgencyService.findAllList(null);
    }

    /**
     * 根据单位Id或者编码删除数据。
     * 开启逻辑删除时，自动逻辑删除。
     * http://localhost:8080/basAgency/removeByWrapper?agencyCode=001003
     * http://localhost:8080/basAgency/removeByWrapper?agencyId=6
     *
     * @param agencyCode ：单位编码
     * @param agencyId   ：单位ID
     * @return
     */
    @GetMapping("/basAgency/removeByWrapper")
    public Map<String, Object> removeByWrapper(@RequestParam(required = false) String agencyCode,
                                               @RequestParam(required = false) Integer agencyId) {
        Map<String, Object> build;
        if (StrUtil.isBlank(agencyCode) && agencyId == null) {
            build = MapUtil.builder(new HashMap<String, Object>()).put("code", 400).put("msg", "参数错误").build();
            return build;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StrUtil.isNotBlank(agencyCode), "agency_code", agencyCode)
                .eq(agencyId != null, "agency_id", agencyId);
        // ==> Preparing: UPDATE BAS_AGENCY SET IS_DELETE=1 WHERE IS_DELETE=2 AND (agency_code = ? AND agency_id = ?)
        // ==> Parameters: 001003(String), 6(Integer)
        basAgencyService.remove(queryWrapper);
        build = MapUtil.builder(new HashMap<String, Object>()).put("code", 200).put("msg", "删除成功").build();
        return build;
    }

}
